package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.DishDao;
import com.delivery.www.po.Dish;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DishDaoImpl implements DishDao {
    //获取店铺下的菜品
    @Override
    public List<Dish> getDish(int storeId) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Dish> dishes = null;
        Object[] params = {storeId};
        String sql = "select d.dish_id,d.dish_name,d.price,d.describe from dish d where store_id = ?";
        rs = JdbcUtils.excute(connection, pstm, rs, sql, params);
        if (rs != null) {
            dishes = new ArrayList<>();
        }
        try {
            while (rs.next()) {
                Dish dish = new Dish();
                dish.setDishId(rs.getInt("dish_id"));
                dish.setDishName(rs.getString("dish_name"));
                dish.setPrice(rs.getFloat("price"));
                dish.setDescribe(rs.getString("describe"));
                dishes.add(dish);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.closeResource(connection, pstm, rs);
        return dishes;
    }

    @Override
    public int addDish(String dishName, int storeId, Float price, String describe) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {dishName, storeId, price, describe};
        String sql = "insert into dish (dish_name,store_id,price,`describe`) values (?,?,?,?)";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        if (excute > 0) {
            Object[] objects = {storeId};
            String s = "update store set dish_num = dish_num + 1 where store_id = ?";
            JdbcUtils.excute(connection, pstm, s, objects);
        }
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }

    @Override
    public int updateDishName(String oldName, String newName) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {newName, oldName};
        String sql = "update dish set dish_name = ? where dish_name = ?";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }

    @Override
    public int updateDishPrice(String oldName, Float price) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {price, oldName};
        String sql = "update dish set price = ? where dish_name = ?";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }

    @Override
    public int updateDishDescribe(String oldName, String introduce) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {introduce, oldName};
        String sql = "update dish set `describe` = ? where dish_name = ?";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }

    @Override
    public int deleteDish(String name, int storeId) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        Object[] params = {name, storeId};
        String sql = "delete from dish where dish_name = ? and store_id = ?";
        int excute = JdbcUtils.excute(connection, pstm, sql, params);
        if (excute > 0) {
            Object[] objects = {storeId};
            String s = "update store set dish_num = dish_num -1 where store_id = ?";
            JdbcUtils.excute(connection, pstm, s, objects);
        }
        JdbcUtils.closeResource(connection, pstm, null);
        return excute;
    }

    @Override
    public Dish getOneDish(int dishId) {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Object[] params = {dishId};
        Dish dish = null;
        String sql = "select d.price from dish d where dish_id = ?";
        rs = JdbcUtils.excute(connection, pstm, rs, sql, params);
        try {
            if (rs.next()) {
                dish = new Dish();
                dish.setPrice(rs.getFloat("price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.closeResource(connection, pstm, rs);
        return dish;
    }
}
